/**
 * @Time    : 2022/2/10 20:30
 * @Author  : WanRongJia
 * @License : Software business license
 * @Site    : www.linshensjl.top
 * @File    : request.js
 * @Software: Rich Web Platform
 * @Function: 接口服务器请求
 * @Version : 1.0
 **/

import store from '../store'
import axios from 'axios'
import { ElMessage } from 'element-plus'
const service = axios.create({
    baseURL: '/api',
    timeout: 10000,
    withCredentials: false
})

service.interceptors.request.use(config => {
    config.headers = {
        Authorization: store.state.accessToken ? store.state.accessToken : undefined,
        Accept: 'application/json',
        'Content-Type': 'application/json'
    }
    return config
}, error => {
    return Promise.reject(error)
})

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return response.data
        } else {
            return Promise.resolve(response.status)
        }
    },
    error => {
        ElMessage({
            message: error.response.data.errors.msg,
            type: 'error',
        })
        return Promise.resolve(error.response.data)
    }
)

export default service
